package com.zw.pm.module.overview.program;


import com.zw.pm.gen.entity.Category;
import com.zw.pm.gen.entity.Comment;
import com.zw.pm.gen.entity.Program;
import com.zw.pm.gen.entity.ProgramDoc;
import com.zw.pm.gen.mapper.CommentMapper;
import com.zw.pm.gen.mapper.ProgramDocMapper;
import com.zw.pm.gen.mapper.ProgramMapper;
import com.zw.pm.module.config.category.CategoryDao;
import com.zw.pm.util.FileUtils;
import com.zw.pm.util.ShiroUtils;
import com.zw.pm.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

import static com.zw.pm.constant.Constant.STATUS_NORMAL;

/**
 * @Description: 会议 服务层
 *
 * @Author: China.sgl
 * @Date: 2019/9/16 09:11
 */
@Service
public class ProgramService {

    @Autowired
    private ProgramMapper programMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private ProgramDao programDao;
    @Autowired
    private ProgramDocDao programDocDao;
    @Autowired
    private ProgramDocMapper programDocMapper;
    @Autowired
    private CategoryDao categoryDao;

    @Value("${user.upload.path}")
    private String uploadPath;

    /**
     * 根据条件查询会议
     *
     * @param programModel 查询条件
     * @return 会议列表数据
     */
    public List<ProgramModel> findAll(ProgramModel programModel){
        programModel.setUserId(ShiroUtils.getUserId());
        return programDao.findAll(programModel);
    }

    /**
     * 保存会议
     * @param program program
     * @param file
     * @return 行数
     */
    public int saveProgram(Program program, MultipartFile[] file) {
        program.setCreateTime(new Date());
        program.setStatus(STATUS_NORMAL);
        program.setUserId(ShiroUtils.getUserId());
        int row = programMapper.insert(program);
        saveFile(program, file);
        return row;
    }

    /**
     * 保存文件
     *
     * @param program program
     * @param file file
     */
    private void saveFile(Program program, MultipartFile[] file) {
        if (null != file && file.length > 0) {
            for (MultipartFile f : file) {
                String path = uploadPath + "program/" + program.getId() + "/";
                String fileName = FileUtils.upload(f, path);
                if (StringUtils.isNotNull(fileName)) {
                    String url = "/files/program/" + program.getId() + "/" + fileName;
                    if (programDocDao.countByUrl(url) > 0) {
                        continue;
                    }
                    ProgramDoc doc = new ProgramDoc();
                    doc.setName(fileName);
                    doc.setUrl(url);
                    doc.setPath(path + fileName);
                    doc.setProgramId(program.getId());
                    doc.setStatus(STATUS_NORMAL);
                    doc.setUploadTime(new Date());
                    doc.setUserId(ShiroUtils.getLoginName());
                    programDocMapper.insert(doc);
                }
            }
        }
    }

    /**
     * 根据ID查询
     * @param id id
     * @return Program
     */
    public ProgramModel getById(Integer id) {
        return programDocDao.getById(id);
    }

    /**
     * 修改
     *
     * @param program program
     * @param file file
     * @return 行数
     */
    public int updateProgram(Program program, MultipartFile[] file) {
        int row = programMapper.updateByPrimaryKeySelective(program);
        saveFile(program, file);
        return row;
    }

    /**
     * 根据会议ID 查询评论
     * @param id 会议ID
     * @return list
     */
    public List<CommentModel> findCommentByProgramId(Integer id) {
        return programDao.findCommentByProgramId(id);
    }

    /**
     * 评价会议
     * @param comment comment
     * @return 行数
     */
    public int saveComment(Comment comment) {
        comment.setDate(new Date());
        comment.setUserId(ShiroUtils.getUserId());
        return commentMapper.insert(comment);
    }

    /**
     * 删除用户评价
     * @param id id
     * @return 行数
     */
    public int delComment(Integer id) {
        return commentMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据ID查询文件
     *
     * @param id id
     * @return ProgramDoc
     */
    public ProgramDoc getFileById(Integer id) {
        return programDocMapper.selectByPrimaryKey(id);
    }

    /**
     * 修改ProgramDoc
     *
     * @param doc ProgramDoc
     * @return 行数
     */
    public int updateDoc(ProgramDoc doc) {
        return programDocMapper.updateByPrimaryKeySelective(doc);
    }

    /**
     * 查询会议类型
     *
     * @return list
     */
    public List<Category> findCategories() {
        return categoryDao.findCategoryAll(null);
    }

}
